package com.zq.myapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONObject;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        volley_Get();
        //volley_Post();
    }


    @Override
    protected void onStop() {
        super.onStop();
        //通过给定的tag值，将指定的队列全部关闭
        MyApplication.getHttpQueues().cancelAll("abcGet");
    }

    //post请求
    private void volley_Post() {
        String url = "http://apis.juhe.cn/mobile/get?";
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("phone", "13429667914");
        hashMap.put("key", "335adcc4e891ba4e4be6d7534fd54c5d");
        JSONObject object = new JSONObject(hashMap);
        JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.POST, url,
                object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Toast.makeText(MainActivity.this,"成功"+jsonObject.toString(),Toast.LENGTH_LONG).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this,"失败"+volleyError.toString(),Toast.LENGTH_LONG).show();
            }
        });
        objectRequest.setTag("abcPost");
        MyApplication.getHttpQueues().add(objectRequest);//添加到队列中




        /*StringRequest request = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {//请求成功
                Toast.makeText(MainActivity.this,"成功"+s,Toast.LENGTH_LONG).show();
            }
        }, new Response.ErrorListener() {//请求失败
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this,"失败"+volleyError.toString(),Toast.LENGTH_LONG).show();
            }
        }){
            @Override//getParams()方法，用户在Volley中使用post方式请求数据中参数的传递
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> hashMap = new HashMap<>();
                hashMap.put("phone","13429667914");
                hashMap.put("key","335adcc4e891ba4e4be6d7534fd54c5d");
                return hashMap;
            }
        };
        request.setTag("abcPost");
        MyApplication.getHttpQueues().add(request);//添加到队列中*/
    }

    //get请求
    private void volley_Get() {
        String url = "http://apis.juhe.cn/mobile/get?phone=13429667914&key=335adcc4e891ba4e4be6d7534fd54c5d";
        VolleryRequest.RequestGet(this, url, "abcGet", new VolleyInterface(this,
                VolleyInterface.mListener,VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                Toast.makeText(MainActivity.this,"成功"+result,Toast.LENGTH_LONG).show();
            }

            @Override
            public void onMyError(VolleyError error) {
                Toast.makeText(MainActivity.this,"失败"+error.toString(),Toast.LENGTH_LONG).show();
            }
        });
    }
}
